package com.zxmc.server.service.impl;

import com.zxmc.common.constant.MessageConstant;
import com.zxmc.common.context.BaseContext;
import com.zxmc.common.exception.AccountNotFoundException;
import com.zxmc.common.exception.FriendBeenException;
import com.zxmc.pojo.dto.AddFriendDto;
import com.zxmc.pojo.dto.DeleteFriendDto;
import com.zxmc.pojo.entity.Friend;
import com.zxmc.pojo.entity.User;
import com.zxmc.server.mapper.FriendMapper;
import com.zxmc.server.service.FriendService;
import com.zxmc.server.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private UserService userService;
    @Override
    public List<Friend> list(String name) {
        Integer userId = BaseContext.getCurrentId();
        return friendMapper.listById(userId,name);
    }

    @Override
    public void add(AddFriendDto addFriendDto) {
        String name=addFriendDto.getName();
        User user = userService.listByName(name);
        User self = userService.list();

        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if(self.getId().equals(user.getId())){
            //不能添加自己
            throw new FriendBeenException(MessageConstant.NOT_SELF);
        }
        List<Friend> list = list(name);
        if (!list.isEmpty()) {
            //已经是好友
            throw new FriendBeenException(MessageConstant.FRIEND_BEEN);
        }
        Friend friend = new Friend();
        friend.setUserId(BaseContext.getCurrentId());
        friend.setFriendId(user.getId());
        friend.setFriendName(name);
        friend.setFriendAvatar(user.getAvatar());
//        friend.setCreatedAt(LocalDateTime.now());
        //互相添加
        friendMapper.add(friend);
        friend.setUserId(user.getId());
        friend.setFriendId(BaseContext.getCurrentId());
        friend.setFriendName(self.getUsername());
        friend.setFriendAvatar(self.getAvatar());
        friendMapper.add(friend);
    }

    @Override
    public void delete(DeleteFriendDto deleteFriendDto) {
        String name=deleteFriendDto.getName();
        User user = userService.listByName(name);
        User self = userService.list();

        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if(self.getId().equals(user.getId())){
            //不能删除自己
            throw new FriendBeenException(MessageConstant.NOT_SELF_DELETE);
        }
        List<Friend> list = list(name);
        if (list.isEmpty()) {
            //不是好友
            throw new FriendBeenException(MessageConstant.FRIEND_NOT);
        }

        friendMapper.delete(self.getId(),user.getId());
    }

    @Override
    public void update(User user) {
        Friend friend = new Friend();
        friend.setFriendAvatar(user.getAvatar());
        friend.setFriendName(user.getUsername());
        friend.setFriendId(user.getId());
        friendMapper.update(friend);
    }

    @Override
    public void deleteCommon(User user) {
        friendMapper.delete(null,user.getId());
    }
}
